﻿using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Card.Make.Pro.Application.Dapper;
using Card.Make.Pro.Application.Helper;
using Card.Make.Pro.Application.Model;
using DevExpress.XtraEditors;
using Microsoft.Data.ConnectionUI;

namespace Card.Make.Pro.Form
{
    public partial class FrmLogin : DevExpress.XtraBars.ToolbarForm.ToolbarForm
    {
        public FrmLogin()
        {
            InitializeComponent();

            _dapperClient = new DapperClient(new Config() { ConnectionString = Properties.Settings.Default.ConnectionString, DbType = DbStoreType.SqlServer });

        }


        /// <summary>
        /// dapper client
        /// </summary>
        private readonly DapperClient _dapperClient;


        /// <summary>
        /// 数据库设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butDataBaseSetting_ItemClick(object sender, ItemClickEventArgs e)
        {
            SettingDataBaseConfig();
        }


        /// <summary>
        /// 设置数据库配置
        /// </summary>
        private void SettingDataBaseConfig()
        {
            var dialog = new DataConnectionDialog();
            dialog.DataSources.Add(DataSource.SqlDataSource);
            dialog.SelectedDataSource = DataSource.SqlDataSource;
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
            dialog.ConnectionString = Properties.Settings.Default.ConnectionString;
            if (DataConnectionDialog.Show(dialog) == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.ConnectionString = dialog.ConnectionString;
                Properties.Settings.Default.Save();
                _dapperClient.CurrentConnectionConfig.ConnectionString = dialog.ConnectionString;
            }
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butLogin_Click(object sender, EventArgs e)
        {
            var pass = this.dxvpLoginValidation.Validate();
            if (!pass)
            {
                return;
            }

            var strConnectionString = Properties.Settings.Default.ConnectionString;
            if (string.IsNullOrWhiteSpace(strConnectionString))
            {
                XtraMessageBox.Show("请先设置数据库连接", "登录消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            LoadingHelper.Open();
            Login();
            LoadingHelper.Close();
        }

        /// <summary>
        /// 登录
        /// </summary>
        private void Login()
        {
            var userName = txtUserName.Text.Trim();
            var password = txtPassword.Text.Trim();


            var sql = $"select * from [Account_Table] where [Account]='{userName}'";
            var data = _dapperClient.QueryFirst<LoginInfo>(sql);
            if (null == data)
            {
                XtraMessageBox.Show("账号不存在", "登录消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!data.PWD.Equals(password))
            {
                XtraMessageBox.Show("密码不正确", "登录消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AppConfig.CurrentLoginInfo = data;

            this.DialogResult = DialogResult.OK;

        }

        private void FrmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                butLogin_Click(sender, e);
            }
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}